//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_TOUCH_H__
#define __ELASTOS_TOUCH_H__

#include <ddk.h>
#include "nu_api.h"
#include "zenr_api.h"
#include "drvassistant.h"

#define RANGE   5
#define BUFFERSIZE 128
#define DELAYTIME 200

void CDECL TouchTimerRoutine(void * parameter);

struct TouchData {
    Int16 dir;
    Int16 x;
    Int16 y ;
};

class CTouch : public CDeviceDriver {
public:
    CTouch() : m_CalibrationEvent(FALSE, UNSIGNALED){
        m_CalibrationPoint = -1;
        m_takeBack = 1;
        m_press = FALSE;
        m_push = FALSE;
        m_head = 0;
        m_tail = 0;
        m_index = 1;
        m_bRead = FALSE,

        m_timer = NULL;
        m_bValidInput = TRUE;
        m_RecogRectX0 = 0;
        m_RecogRectY0 = 0;
        m_RecogRectX1 = 0;
        m_RecogRectY1 = 0;
        m_beingChangeMode = FALSE;
    }

    ECode Init() {
        m_timer = new DzTimer(DELAYTIME, TouchTimerRoutine, (void *)this);
        if (!m_timer) {
            DRV_EXPRESSION(ERRORLEVEL, kprintf("Create timer failed\n"));

            return E_OUT_OF_MEMORY;
        }

        return NOERROR;
    }

    CARAPI Read(
        /* [in] */ Int64 offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf* pBuffer,
        /* [out] */ IEvent** ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 offset,
        /* [in] */ const MemoryBuf &buffer,
        /* [out] */ Int32* pBytesWritten,
        /* [out] */ IEvent** ppCompletionEvent) {
        return E_NOT_IMPLEMENTED;
    }

    CARAPI Control(
        /* [in] */ Handle32 controlCode,
        /* [in] */ const MemoryBuf &inBuffer,
        /* [out] */ MemoryBuf* pOutBuffer,
        /* [out] */ IEvent** ppCompletionEvent);
    void InitTouchState();
    virtual void Dispose() {}

public:
    DzMutex m_mutex;

    DzEvent m_event;
    DzEvent m_CalibrationEvent;
    Int32 m_CalibrationPoint;
    Int32 m_takeBack;

    Boolean m_press;
    Boolean m_push;
    UInt32 m_head;
    UInt32 m_tail;

    Int32 m_index;
    Int32 m_bRead;

    DzTimer* m_timer;

    Boolean  m_bValidInput;
    Int32    m_RecogRectX0;
    Int32    m_RecogRectY0;
    Int32    m_RecogRectX1;
    Int32    m_RecogRectY1;
    UInt32   m_beingChangeMode;
};

#endif // __ELASTOS_TOUCH_H__

